package cn.edu.buaa.treehole.dao;

import cn.edu.buaa.treehole.dao.exception.DaoException;
import cn.edu.buaa.treehole.dao.exception.PidNotExistException;
import cn.edu.buaa.treehole.dao.exception.RidHasExistException;
import cn.edu.buaa.treehole.dao.exception.RidNotExistException;
import cn.edu.buaa.treehole.pojo.ReplyInfo;

/*
 * @author Deng XinYu
 * */
public interface ReplyDao {
    /*
     * normal_behaviour:
     *   Pre-Condition:
     *       contain(replyInfo.getPid(), replyInfo.getRid()) == false;
     *   Post-Condition:
     *       \suggest PostInfo info = { select * from reply_table where PID=replyInfo.getPid() and RID=replyInfo.getRid() };
     *       info != None;
     *       info.equals(postInfo) == true;
     *
     * exception_behaviour:
     *   Pre-Condition:
     *       contain(replyInfo.getPid(), replyInfo.getRid()) == true;
     *   \raise RidHasExistException;
     * */
    void addReply(ReplyInfo replyInfo) throws RidHasExistException, DaoException;

    /*
     * normal_behaviour:
     *   Pre-Condition:
     *       contain(pid, rid) == true;
     *   Post-Condition:
     *       \result == { select * from reply_table where PID=pid and RID=rid };
     *
     * exception_behaviour:
     *   Pre-Condition:
     *       contain(replyInfo.getPid(), replyInfo.getRid()) == false;
     *   \raise RidNotExistException;
     * */
    ReplyInfo getReply(long pid, long rid) throws RidNotExistException, DaoException;

    /*
     * normal_behaviour:
     *   Pre-Condition:
     *       \old(this).contain(pid, rid) == true;
     *   Post-Condition:
     *       \after(this).contain(pid, rid) == false;
     *
     * exception_behaviour:
     *   Pre-Condition:
     *       \old(this).contain(pid, rid) == false;
     *   \raise RidNotExistException;
     * */
    void deleteReply(long pid, long rid) throws RidNotExistException, DaoException;

    /*
     * Post-Condition:
     *   \result == { select * from reply_table where PID=pid }.size();
     * */
    int getRepliesCount(long pid) throws DaoException;

    /*
     * Post-Condition:
     *   \if { select * from post_table where PID=pid and RID=rid } != None, \result == true,
     *   \else, \result == false;
     * */
    boolean contain(long pid, long rid) throws DaoException;

    /*
    * normal_behaviour:
    *   Pre-Condition:
    *       { select * from reply_table where PID=pid }.size() != 0;
    *   Post-Condition:
    *       \for_all long rid \in { select PID from post_table where PID=pid }
    *           getReply(pid).getDate().comparedTo(getReply(\result).getDate()) <= 0;
    *
    * exception_behaviour:
    *   Pre-Condition:
    *       { select * from reply_table where PID=pid }.size() == 0;
    *   \raise RidNotExistException;
    * */
    long getLatestReplyRid(long pid) throws RidNotExistException, DaoException;

    /*
     * normal_behaviour:
     *   Pre-Condition:
     *       { select * from reply_table where PID=pid }.size() != 0;
     *   Post-Condition:
     *       \for_all long rid \in { select PID from post_table where PID=pid }
     *           getReply(pid).getDate().comparedTo(getReply(\result).getDate()) >= 0;
     *
     * exception_behaviour:
     *   Pre-Condition:
     *       { select * from reply_table where PID=pid }.size() == 0;
     *   \raise RidNotExistException;
     * */
    long getEarliestReplyRid(long pid) throws RidNotExistException, DaoException;
}